package com.dg11185.nearshop.net.request;

import org.json.JSONException;
import org.json.JSONObject;

import com.dg11185.libs.network.http.client.HttpRequest;
import com.dg11185.libs.utils.MD5.MD5Utils;
import com.dg11185.nearshop.app.Constants;
import com.dg11185.nearshop.net.response.NetStatus;

/**
 * 
 * 返回数据中只需要获取一个状态码的时候可以使用该类
 * 
 * @author：Daibo
 * @time：2014-5-24 下午1:27:35
 */
public class GeneralRequest extends HttpRequest<NetStatus> {

    public static final String LIKE = "behavior/like";// 点赞

    public static final String CANCEL_LIKE = "behavior/cancelLike";// 取消赞

    public static final String FAVOR = "behavior/favor";// 收藏

    public static final String CANCEL_FAVOR = "behavior/cancelFavor";// 取消收藏

    public static final String SCAN_QR_CODE = "behavior/scanQRCodeLog";// 扫描二维码

    public static final String ADD_COMMENT = "merchant/commentMerchant";// 添加主评论

    public static final String REPLY_COMMENT = "merchant/replyComment"; // 回复评论

    public static final String MODIFY_DETAIL = "member/modifyDetail"; // 修改会员详情

    public static final String MODIFY_SIGNATURE = "member/modifySignature";// 修改签名

    public static final String LOCATION_UPDATE = "merchant/locationUpdate";// 商家位置校对

    //
    //
    //
    //

    public GeneralRequest(String address) {
        super(Constants.ADDRESS, address);
    }

    /**
     * 添加可选参数
     */
    public GeneralRequest add(String key, String value) {
        addParam(key, value);
        return this;
    }

    public GeneralRequest encryption() {
        addParam("chshToken", Constants.TOKEN);
        addParam("chshSign", MD5Utils.getMD5Str(getParams(), Constants.MD5_key));
        return this;
    }

    @Override
    public NetStatus parseJson(String response){
        NetStatus status = null;

        try {
            JSONObject object = new JSONObject(response);
            status = NetStatus.getStatus(object.optString("status"));
        } catch (JSONException e) {
            e.printStackTrace();
        } finally {
            if (status == null) {
                status = NetStatus.EXCEPTION;
            }
        }
        return status;
    }

}
